package com.example.easyscript.utils.recognization.surfpro;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.highgui.Highgui;


public class FeatureMatching {
    private Mat src;
    private MatOfKeyPoint srcKeyPoints;
    private Mat srcDes;

    private FeatureDetector detector;
    private DescriptorExtractor extractor;
    private DescriptorMatcher matcher;

    private MatchingView view;
    public FeatureMatching(MatchingView view) {
        this.view = view;
        srcKeyPoints = new MatOfKeyPoint();
        srcDes = new Mat();
        detector = FeatureDetector.create(FeatureDetector.SURF);
        extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
        matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
    }

    public int doMaping(String dstPath) {
        view.setDstPic(dstPath);
        // 读入待测图像
        Mat dst = Highgui.imread(dstPath);
        //System.out.println("DST W:"+dst.cols()+" H:" + dst.rows());
        // 待测图像的关键点
        MatOfKeyPoint dstKeyPoints = new MatOfKeyPoint();
        detector.detect(dst, dstKeyPoints);
        // 待测图像的特征矩阵
        Mat dstDes = new Mat();
        extractor.compute(dst, dstKeyPoints, dstDes);
        // 与原图匹配

        MatOfDMatch matches = new MatOfDMatch();
        matcher.match(dstDes, srcDes, matches);
        //将结果输入到视图 并得到“匹配度”
        return view.showView(matches, srcKeyPoints, dstKeyPoints);
    }

    public void setSource(String srcPath) {
        view.setSrcPic(srcPath);
        // 读取图像 写入矩阵
        src = Highgui.imread(srcPath);
        //System.out.println("SRC W:"+src.cols()+" H:" + src.rows());
        // 检测关键点
        detector.detect(src, srcKeyPoints);
        // 根据源图像、关键点产生特征矩阵数值
        extractor.compute(src, srcKeyPoints, srcDes);
    }

}